<template>
  <div class="p-2">

    <div class="mb-[10px]">
      <el-card>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          label-width="auto"
        >
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 240px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            label="企业编码"
            prop="no"
          >
            <el-input
              v-model="queryParams.no"
              placeholder="请输入企业编码"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="企业名称"
            prop="name"
          >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入企业名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="业务员名称"
            prop="scaleName"
          >
            <el-input
              v-model="queryParams.scaleName"
              placeholder="请输入业务员名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleQuery"
              icon="Search"
              v-hasPermi="['payment:merchant:list']"
            >查询</el-button>
            <el-button
              @click="resetQuery"
              icon="Refresh"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              @click="addAndEditEnterprise('add')"
              v-hasPermi="['payment:merchant:add']"
              icon="Plus"
            >新增</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table
        v-loading="loading"
        :data="enterpriseList"
        border
      >
        <el-table-column
          label="创建时间"
          align="center"
          min-width="160"
          prop="createTime"
        />
        <el-table-column
          label="企业名称"
          align="center"
          min-width="160"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="企业编码"
          align="center"
          min-width="160"
          prop="no"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="所属代理商"
          align="center"
          min-width="160"
          prop="agentName"
        />
        <el-table-column
          label="所属代理商编号"
          align="center"
          min-width="160"
          prop="agentNo"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="所属运营商"
          align="center"
          min-width="160"
          prop="operatorName"
        />
        <el-table-column
          label="所属运营商编号"
          align="center"
          min-width="160"
          prop="operatorNo"
        />
        <el-table-column
          label="业务员"
          align="center"
          min-width="160"
          prop="scaleName"
        />
        <el-table-column
          label="业务员编号"
          align="center"
          min-width="160"
          prop="scaleNo"
        />
        <el-table-column
          label="审核状态"
          align="center"
          min-width="160"
          prop="verifyStatusName"
        />

        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          :width="colWidth"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tag
              v-if="scope.row.verifyStatusName !='审核通过'"
              link
              @click="addAndEditEnterprise('edit',scope.row)"
              v-hasPermi="['payment:channelSalesman:edit']"
            >信息修改</el-tag>
            <el-tag
              v-if="scope.row.verifyStatusName =='待审核' && roleId== 5"
              link
              type="success"
              @click="checkMer(scope.row)"
              v-hasPermi="['payment:merchant:check']"
            >审核</el-tag>
            <template v-if="scope.row.isCanSetScale == 1">
              <el-tag
                link
                @click="handleAdd(scope.row)"
                v-if="!scope.row.scaleName "
                v-hasPermi="['payment:merchant:setYwy']"
              >配置业务员</el-tag>
              <el-tag
                v-if="scope.row.scaleName"
                link
                type="danger"
                @click="ubYwyFun(scope.row)"
                v-hasPermi="['payment:merchant:unbindYwy']"
              >解绑业务员</el-tag>
            </template>
            <!--
            <el-tag
              link
              v-if="scope.row.verifyStatusName !='审核通过'"
              @click="addAndEditEnterprise('edit',scope.row)"
              v-hasPermi="['payment:merchant:edit']"
            >编辑</el-tag>-->
            <el-tag
              link
              @click="addAndEditEnterprise('look',scope.row)"
              v-hasPermi="['payment:merchant:query']"
            >详情</el-tag>
            <el-tag @click="virtualAccountFun(scope.row)"  v-hasPermi="['payment:merchantAccount:createXn']"  v-if="roleId== 5">虚拟账户</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改业务员配置对话框 -->
    <el-dialog
      ref="formDialogRef"
      title="配置业务员"
      v-model="visible"
      width="600px"
      append-to-body
      @close="closeDialog"
    >
      <el-form
        :model="form"
        ref="userFormRef"
        label-width="auto"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="业务员"
              prop="ywyId"
              required
            >
              <el-select
                v-model="form.ywyId"
                placeholder="请选择业务员"
                filterable
              >
                <el-option
                  v-for="(item,i) in salesmanList"
                  :label="item.name"
                  :value="item.id"
                  :key="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel()">关 闭</el-button>
          <el-button
            type="primary"
            @click="submitForm"
          >确 定</el-button>
        </div>
      </template>
    </el-dialog>
    <VirtualAccount ref="virtualAccountRef"></VirtualAccount>

    <Check ref="agentCheckRef" :title="checkParams.title" :openCheck="checkParams.openCheck" :item="checkParams.item" :id="checkParams.id" @dialogClose="dialogClose" @dialogSubmit="dialogSubmit"></Check>

  </div>
</template>

<script setup name="addEnterprise" lang="ts">
import api from '@/api/enterprise/addEnterprise';
import { EnterpriseQuery, EnterpriseVO, SetYwyForm } from '@/api/enterprise/addEnterprise/types';
import { SalesmanVO } from '@/api/system/salesman/types';
import useUserStore  from '@/store/modules/user'
import Check from "@/components/customDialog/check/index.vue"

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const enterpriseList = ref<EnterpriseVO[]>();
const loading = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const queryParams = ref<EnterpriseQuery>({
  pageNum: 1,
  pageSize: 10,
  scaleName: '',
  name: '',
  no: ''
});


const agentCheckRef = ref();
const checkParams = ref({
  'title':'企业审核',
  openCheck:false,
  item:{},
  id:''
});


const checkMer = (row:any)=>{
  checkParams.value.openCheck = true;
  checkParams.value.item = row;
  checkParams.value.id = row.id;
}


const dialogClose = ()=>{
  checkParams.value.openCheck = false;
}



const dialogSubmit = (params:any)=>{
  let subParams = {
    merId:params.id,
    checkType:params.checkType,
    reason:params.remarer,
    domain:location.origin
  }
  api.checkMerchant(subParams).then(res=>{
    agentCheckRef.value.hideLoad();
    dialogClose();
    getList();
  })
}


const roleId = ref();
roleId.value = useUserStore().curRoleId;
const colWidth = ref(260);





/** 查询企业列表 */
const getList = async () => {
  loading.value = true;
  const res = await api.enterpriseList(proxy?.addDateRange(queryParams.value, dateRange.value));
  loading.value = false;
  enterpriseList.value = res.rows;
  total.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  handleQuery();
};

/** 新增&修改 */
const addAndEditEnterprise = (type: string, row: any = '') => {
  router.push(type == 'look' ? 'enterpriseDetail?id=' + row.id : type == 'edit' ? 'enterpriseEdit?id=' + row.id : 'enterpriseAdd');
};

/** 配置业务员 */
const visible = ref(false);
const salesmanList = ref<SalesmanVO[]>();
const form = ref<SetYwyForm>({
  id: '',
  ywyId: ''
});
const rules = {
  ywyId: [{ required: true, message: '请选择业务员', trigger: 'blur' }]
};
const userFormRef = ref<ElFormInstance>();
const formDialogRef = ref<ElDialogInstance>();
/** 重置操作表单 */
const reset = () => {
  form.value = {
    id: '',
    ywyId: ''
  };
  userFormRef.value?.resetFields();
};

/** 取消按钮 */
const cancel = () => {
  visible.value = false;
  reset();
};
/** 配置业务员 */
const handleAdd = async (row: any) => {
  reset();
  visible.value = true;
  let res = await api.channelSalesman();
  console.log(res);
  form.value.id = row.id;
  salesmanList.value = res.data;
};
/** 提交按钮 */
const submitForm = () => {
  userFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      let res = await api.setYwy(form.value);
      proxy?.$modal.msgSuccess(res.msg);
      visible.value = false;
      await getList();
    }
  });
};
/**
 * 关闭业务员弹窗
 */
const closeDialog = () => {
  visible.value = false;
  resetForm();
};
/**
 * 重置表单
 */
const resetForm = () => {
  userFormRef.value?.resetFields();
  userFormRef.value?.clearValidate();
};

/** 解绑业务员 */
const ubYwyFun = async (row: any) => {
  let res = await api.ubYwy(row.id);
  proxy?.$modal.msgSuccess(res.msg);
  await getList();
};

/** 虚拟账户 */
const virtualAccountRef = ref<any>();
const virtualAccountFun = (row: any) => {
  virtualAccountRef.value.virtualAccountVisibleFun('enterprise', row);
};

onMounted(() => {
  getList(); // 初始化列表数据

  // if(roleId.value  == 6 || roleId.value == 7){

  // }
});
</script>

<style lang="scss" scoped></style>
